/*
 * Copyright (C) 2013 Daniel Wasilew <daniel.wasilew@codeminer.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.codeminer.parts.database.dao;

import org.codeminer.database.DatabaseConnection;
import org.codeminer.database.DatabaseException;
import org.codeminer.parts.database.sql.SQL;
import org.codeminer.parts.server.users.User;

import java.util.LinkedList;
import java.util.List;

/**
 * @author Daniel Wasilew <daniel.wasilew@codeminer.org>
 */
public class UserDAO extends DAO {

    public static String getUserPassword(User user, DatabaseConnection connection) throws DatabaseException {
        String query = SQL.select("User_Pass").from("USERS").where("User_Name").is(user.getName()).toSQL();
        try {
            connection.accuireReadLock();
            return getSingleProperty(query, "User_Pass", connection);

        } finally {
            connection.releaseReadLock();
        }
    }

    public static List<User> getAll(DatabaseConnection connection) throws DatabaseException {
        String query = SQL.select("User_Name").from("USERS").toSQL();
        try {
            connection.accuireReadLock();
            List<String> propertyList = getPropertyList(query, "User_Name", connection);
            List<User> users = new LinkedList<>();
            for (String name : propertyList) {
                users.add(new User(name));
            }
            return users;
        } finally {
            connection.releaseReadLock();
        }
    }

    public static boolean delete(User user, DatabaseConnection connection) throws DatabaseException {
        String deleteStatement = SQL.delete().from("USERS").where("User_Name").is(user.getName()).toSQL();
        try {
            connection.accuireReadLock();
            return executeUpdate(connection, deleteStatement);
        } finally {
            connection.releaseReadLock();
        }
    }

    public static User store(User user, String password, DatabaseConnection connection) throws DatabaseException {
        String insertStatement = SQL.insertInto("USERS").values().value(user.getName()).value(password).toSQL();
        try {
            connection.accuireWriteLock();
            executeInsert(connection, insertStatement);
            return user;
        } finally {
            connection.releaseWriteLock();
        }
    }

    public static boolean update(User user, String newPassword, DatabaseConnection connection) throws DatabaseException {
        String update = SQL.update("USERS").set("User_pass", newPassword).where("User_Name").is(user.getName()).toSQL();
        try {
            connection.accuireWriteLock();
            return executeUpdate(connection, update);
        } finally {
            connection.releaseWriteLock();
        }

    }

    public static User getUser(String name, DatabaseConnection connection) throws DatabaseException {
        String query = SQL.select("User_Name").from("USERS").where("User_Name").is(name).toSQL();
        try {
            connection.accuireReadLock();
            String userName = getSingleProperty(query, "User_Name", connection);
            if (userName != null) {
                return new User(name);
            } else {
                throw new DatabaseException("no user with name [" + name + "] found");
            }
        } finally {
            connection.releaseReadLock();
        }
    }
}
